﻿using ProjectFootball.MVC.Foundation;
using ProjectFootball.MVC.Foundation.ResourceLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Security.Principal;

namespace ProjectFootball.MVC.Models.EntityFramework.Entities
{
    public class Match : BaseApprovableEntity, IApprovableEntity
    {
        #region Constructors
        public Match()
        {
            MatchEvents = new HashSet<MatchEvent>();
        }

        public Match(IPrincipal user, CreateType createType, Guid approvalGuid, DateTime matchDate, Guid competitionGuid, Guid? venueGuid, long? attendance, Guid team1Guid, short? team1Ht, short? team1Ft, short? team1EtHt, short? team1EtFt, short? team1Pens, Guid team2Guid, short? team2Ht, short? team2Ft, short? team2EtHt, short? team2EtFt, short? team2Pens)
            :base(user, createType, approvalGuid)
        {
            MatchDate = matchDate;
            CompetitionGuid = competitionGuid;
            VenueGuid = venueGuid;
            Attendance = attendance;
            Team1Guid = team1Guid;
            Team1HT = team1Ht;
            Team1FT = team1Ft;
            Team1ETHT = team1EtHt;
            Team1ETFT = team1EtFt;
            Team1Pens = team1Pens;
            Team2Guid = team2Guid;
            Team2HT = team2Ht;
            Team2FT = team2Ft;
            Team2ETHT = team2EtHt;
            Team2ETFT = team2EtFt;
            Team2Pens = team2Pens;
        }
        #endregion

        public DateTime MatchDate { get; set; }

        [ForeignKey("Competition")]
        public Guid CompetitionGuid { get; set; }

        [ForeignKey("Venue")]
        public Guid? VenueGuid { get; set; }

        public long? Attendance { get; set; }

        [ForeignKey("Team1")]
        public Guid Team1Guid { get; set; }

        public short? Team1HT { get; set; }
        public short? Team1FT { get; set; }
        public short? Team1ETHT { get; set; }
        public short? Team1ETFT { get; set; }
        public short? Team1Pens { get; set; }

        [ForeignKey("Team2")]
        public Guid Team2Guid { get; set; }

        public short? Team2HT { get; set; }
        public short? Team2FT { get; set; }
        public short? Team2ETHT { get; set; }
        public short? Team2ETFT { get; set; }
        public short? Team2Pens { get; set; }

        [ForeignKey("CompetitionGuid")]
        public virtual Competition Competition { get; set; }

        [ForeignKey("VenueGuid")]
        public virtual Venue Venue { get; set; }

        [InverseProperty("Team1Matches")]
        public virtual Team Team1 { get; set; }

        [InverseProperty("Team2Matches")]
        public virtual Team Team2 { get; set; }

        [ForeignKey("MatchGuid")]
        public virtual ICollection<MatchEvent> MatchEvents { get; set; }

        #region NotMapped
        [NotMapped]
        public TeamVersion TeamVersion1
        {
            get { return Team1.TeamVersions.Single(t => t.ActiveFrom <= MatchDate && t.ActiveTo >= MatchDate); }
        }

        [NotMapped]
        public TeamVersion TeamVersion2
        {
            get { return Team2.TeamVersions.Single(t => t.ActiveFrom <= MatchDate && t.ActiveTo >= MatchDate); }
        }

        [NotMapped]
        public string Description
        {
            get
            {
                if (Team1FT != null && Team2FT != null)
                    return string.Format("{0} {1}-{2} {3}", TeamVersion1.TeamName, Team1FT, Team2FT, TeamVersion2.TeamName);
                else
                    return string.Format("{0} v {1}", TeamVersion1.TeamName, TeamVersion2.TeamName);
            }
        }
        #endregion
    }
}